package com.heima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.GoodsClassMapper;
import com.heima.pojo.GoodsClassName;
import com.heima.pojo.GoodsClassQueryParam;
import com.heima.pojo.PageBean;
import com.heima.service.GoodsClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GoodsClassServiceImpl implements GoodsClassService {
    @Autowired
    private GoodsClassMapper goodsClassMapper;

    /**
     * 商品列表分页查询
     *
     * @param pram
     * @return
     */
    @Override
    public PageBean GoodsClasslist(GoodsClassQueryParam pram) {
        PageHelper.startPage(pram.getPageIndex(), pram.getPageSize());
        List<GoodsClassName> list = goodsClassMapper.getgoodsClasslist(pram);
        // 获取分页信息
        PageInfo<GoodsClassName> pageInfo = new PageInfo<>(list);

        // 将分页信息封装到自定义的 PageBean 中
        return new PageBean(
                pram.getPageIndex(),
                pram.getPageSize(),
                (long) pageInfo.getPages(), // 总页数
                (long) pageInfo.getTotal(), // 总记录数
                pageInfo.getList()         // 当前页的数据列表
        );
    }

    @Override
    public GoodsClassName selectByID(String classId) {

        return goodsClassMapper.selectById(classId);
    }

    @Override
    public boolean upDateClassName(String classId, String className) {
        if (goodsClassMapper.selectByClassName(className) != null) {
            return false;
        }
        goodsClassMapper.upDateClassName(classId, className);
        return true;
    }

    @Override
    public void deleteClassName(String classId) {
        goodsClassMapper.deleteClassName(classId);
    }
}
